package BiTree;

/**
 * @program: leetcode
 * @description:
 * @author: lxf
 * @create: 2024-06-20 23:01
 */
public class MaxDepth {
    private int max = 1;

    public int maxDepth(TreeNode root) {

        if (root == null) {
            return 0;
        }
        maxDepth(root,1);
        return max;
    }

    public void maxDepth(TreeNode root,int num) {
        max = Math.max(max,num);
        if (root.left != null && root.right != null){
            maxDepth(root.left, num+1);
            maxDepth(root.right, num+1);
        }
        if (root.left != null && root.right == null){
            maxDepth(root.left, num+1);
        }
        if (root.right != null && root.left == null){
            maxDepth(root.right, num+1);
        }
    }


    public int maxDepth2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return Math.max(maxDepth2(root.left), maxDepth2(root.right)) + 1;
    }

}
